package com.ruoyi.bole.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bole.domain.BoleEventRegistration;
import com.ruoyi.bole.service.IBoleEventRegistrationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动报名Controller
 * 
 * @author WuJiale
 * @date 2025-03-23
 */
@RestController
@RequestMapping("/bole/registration")
public class BoleEventRegistrationController extends BaseController
{
    @Autowired
    private IBoleEventRegistrationService boleEventRegistrationService;
    //取消报名
    @Log(title = "活动报名", businessType = BusinessType.DELETE)
    @DeleteMapping("/user/{regId}")
    public AjaxResult cancleRegiste(@PathVariable Long regId)
    {

        return toAjax(boleEventRegistrationService.cancleRegiste(regId));
    }
    //活动报名通过与否
    @PreAuthorize("@ss.hasPermi('bole:registration:edit')")
    @Log(title = "活动报名", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public AjaxResult check(@RequestBody BoleEventRegistration boleEventRegistration)
    {

        return toAjax(boleEventRegistrationService.check(boleEventRegistration));
    }


    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping("/user")
    public AjaxResult addUser(@RequestBody BoleEventRegistration boleEventRegistration)
    {
        boleEventRegistration.setUserId(SecurityUtils.getUserId());
        boleEventRegistration.setRegStatus("0");
        return toAjax(boleEventRegistrationService.userRegistration(boleEventRegistration));
    }
    //查询报名信息
    @GetMapping("/registionInfo/{contentId}")
    public AjaxResult info(@PathVariable("contentId") Long contentId)
    {
        System.out.println(contentId);
        //获取登录人的id
        Long userId = SecurityUtils.getUserId();
        BoleEventRegistration registration = boleEventRegistrationService.selectByContentIdAndUserId(contentId,userId);
        return success(registration);
    }
    /**
     * 查询活动报名列表
     */
//    @PreAuthorize("@ss.hasPermi('bole:registration:list')")
    @GetMapping("/list")
    public TableDataInfo list(BoleEventRegistration boleEventRegistration)
    {
        startPage();
        List<BoleEventRegistration> list = boleEventRegistrationService.selectBoleEventRegistrationList(boleEventRegistration);
        return getDataTable(list);
    }

    /**
     * 导出活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('bole:registration:export')")
    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BoleEventRegistration boleEventRegistration)
    {
        List<BoleEventRegistration> list = boleEventRegistrationService.selectBoleEventRegistrationList(boleEventRegistration);
        ExcelUtil<BoleEventRegistration> util = new ExcelUtil<BoleEventRegistration>(BoleEventRegistration.class);
        util.exportExcel(response, list, "活动报名数据");
    }

    /**
     * 获取活动报名详细信息
     */
//    @PreAuthorize("@ss.hasPermi('bole:registration:query')")
    @GetMapping(value = "/{regId}")
    public AjaxResult getInfo(@PathVariable("regId") Long regId)
    {
        return success(boleEventRegistrationService.selectBoleEventRegistrationByRegId(regId));
    }

    /**
     * 新增活动报名
     */
    @PreAuthorize("@ss.hasPermi('bole:registration:add')")
    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BoleEventRegistration boleEventRegistration)
    {
        boleEventRegistration.setUserId(SecurityUtils.getUserId());
        //如果审核id没有值就赋0
        if (boleEventRegistration.getRegStatus() == null) {
            boleEventRegistration.setRegStatus("0");
        }
        return toAjax(boleEventRegistrationService.insertBoleEventRegistration(boleEventRegistration));
    }

    /**
     * 修改活动报名
     */
    @PreAuthorize("@ss.hasPermi('bole:registration:edit')")
    @Log(title = "活动报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BoleEventRegistration boleEventRegistration)
    {
        //到了审核通过要扣积分这一步
        //安全校验   确保只能修改自己的报名信息  如果是admin就不限制,如果改的是别的用户就返回
//        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())){
//            return toAjax(boleEventRegistrationService.updateBoleEventRegistration(boleEventRegistration));
//        }else if (!SecurityUtils.getUserId().equals(boleEventRegistration.getUserId())){
//            return AjaxResult.error("没有权限修改别人的报名信息");
//        }

        return toAjax(boleEventRegistrationService.updateBoleEventRegistration(boleEventRegistration));
    }

    /**
     * 删除活动报名
     */
    @PreAuthorize("@ss.hasPermi('bole:registration:remove')")
    @Log(title = "活动报名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{regIds}")
    public AjaxResult remove(@PathVariable Long[] regIds)
    {

        return toAjax(boleEventRegistrationService.deleteBoleEventRegistrationByRegIds(regIds));
    }
}
